package tq.com.easysurfacecapture.Inner;

import android.media.MediaRecorder;
import android.view.Surface;

import java.io.IOException;

public class TQMediaRecordHelper {
    private MediaRecorder mediaRecorder = null;

    public TQMediaRecordHelper(){

    }

    public boolean initRecoder(Surface surface, float width, float height, String outFile){
        if(mediaRecorder == null){
            mediaRecorder = new MediaRecorder();
        }

        mediaRecorder.reset();

        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setInputSurface(surface);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mediaRecorder.setAudioSamplingRate(44100);
        mediaRecorder.setAudioEncodingBitRate(96000);


        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

        mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setVideoSize((int) width, (int) height);

        mediaRecorder.setOutputFile(outFile);

        try{
            mediaRecorder.prepare();
        }catch (IOException e){
            return false;
        }
        return true;
    }

    public boolean start(){
        if(mediaRecorder != null){
            mediaRecorder.start();
        }
        return true;
    }
    public boolean pause(){
        if(mediaRecorder != null){
            mediaRecorder.pause();
        }
        return true;
    }
    public boolean resume(){
        if(mediaRecorder != null){
            mediaRecorder.resume();
        }
        return true;
    }
    public void stop(){
        if(mediaRecorder != null){
            mediaRecorder.stop();
        }
    }

    public void release(){
        if(mediaRecorder != null){
            mediaRecorder.release();
        }
    }
}
